package scheduler

import "time"

// ConstantSchedule 固定间隔调度器
type ConstantSchedule struct {
	Delay time.Duration // 间隔时间
}

// Every 每隔多少时间
func Every(duration time.Duration) *ConstantSchedule {
	if duration < time.Second {
		duration = time.Second
	}
	return &ConstantSchedule{Delay: duration - time.Duration(duration.Nanoseconds())%time.Second}
}

// Next 下次执行时间
func (s *ConstantSchedule) Next(inTime time.Time) time.Time {
	return inTime.Add(s.Delay - time.Duration(inTime.Nanosecond())*time.Nanosecond)
}
